//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Controls
{
    /// <summary>
    /// Summary description for ViewBlogPost.
    /// </summary>
    public class EntryPostCommentContainer :  WeblogThemedControl
    {
        private EntryView entry = null;
        private CommentForm form = null;  
        private EntryComments comments = null;

        public EntryPostCommentContainer():base()
        {
			
        }

        protected override void AttachChildControls()
        {
            entry = FindControl( "Entry" ) as EntryView;
            form = FindControl( "form" ) as CommentForm;
            comments = FindControl( "Comments" ) as EntryComments;

            if(entry == null || form == null || comments == null)
                return;

            BindData();
        }

        void BindData()
        {
            CSContext csContext = CSContext.Current;
            WeblogPost we = WeblogPosts.GetPost(csContext.PostID,false,true,true);

            if(we != null && CurrentWeblog.SectionID == we.SectionID && CurrentWeblog.EnableNewComments(we,CurrentUser))
            {
               
                entry.DataSource = we;
                entry.DataBind();
                
 
                form.DataSource = we;
                form.DataBind();
 

                // Retrieve comments to populate EntryComments control
                BlogThreadQuery query = new BlogThreadQuery();
                query.PostID = csContext.PostID;

                if(!Globals.IsNullorEmpty(this.CurrentCSContext.PostName))
                    query.PostName = this.CurrentCSContext.PostName;

                query.IncludeCategories = false;
                query.ReturnFullThread = true;
                query.SectionID = CurrentWeblog.SectionID;

                PostSet ps = WeblogPosts.GetPosts(query,true);

                if(ps != null)
                {
                    comments.DataSource = ps;
                    comments.DataBind();
                }
 

                this.SetPageTitle(we.Subject);
            }
            else
            {
                csContext.Context.Response.Redirect(BlogUrls.Instance().Post(we,CurrentWeblog));
                csContext.Context.Response.End();
            }
        }

    }
}
